using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.ETL.Properties;

namespace Workflows.Components.ETL
{
    /// <summary></summary>
    [Serializable]
    public class BFileDownloadComponent:WorkflowComponentBase
    {
        #region props

        private BFileDownloadInstruction _Instruction;
        private BFileDownloadOutput _Output;
        /// <summary></summary>
        public DataProviderType DbProvider
        {
            get { return this._Instruction.DbProvider; }
            set { this._Instruction.DbProvider = value; }
        }
        /// <summary></summary>
        public string ConnStr
        {
            get { return this._Instruction.ConnStr; }
            set { this._Instruction.ConnStr = value; }
        }
        /// <summary></summary>
        public string SelectStatement
        {
            get { return this._Instruction.SelectStatement; }
            set { this._Instruction.SelectStatement = value; }
        }
        /// <summary></summary>
        public string FileNameField
        {
            get { return this._Instruction.FileNameField; }
            set { this._Instruction.FileNameField = value; }
        }
        /// <summary></summary>
        public string BFileField
        {
            get { return this._Instruction.BFileField; }
            set { this._Instruction.BFileField = value; }
        }
        /// <summary></summary>
        public string DownloadToFolder
        {
            get { return this._Instruction.DownloadToFolder; }
            set { this._Instruction.DownloadToFolder = value; }
        }
        /// <summary></summary>
        public string FileNameFormat
        {
            get
            {
                return this._Instruction.FileNameFormat;
            }
            set
            {
                this._Instruction.FileNameFormat = value;
            }
        }
        /// <summary></summary>
        public string LogFilePath
        {
            get
            {
                return this._Instruction.LogFilePath;
            }
            set
            {
                this._Instruction.LogFilePath = value;
            }
        }
        /// <summary></summary>
        public string ErrorFilePath
        {
            get
            {
                return this._Instruction.ErrorFilePath;
            }
            set
            {
                this._Instruction.ErrorFilePath = value;
            }
        }

        public override InstructionBase Instruction
        {
            get { return this._Instruction; }
            set { this._Instruction=(BFileDownloadInstruction)value; }
        }

        public override ResultBase Output
        {
            get { return this._Output; }
            set { this._Output=(BFileDownloadOutput)value; }
        }

        #endregion

        /// <summary></summary>
        public BFileDownloadComponent():base()
        {
            this.ComponentName = "Download BFile";
            this.Description = "download bfile/image contents from database to local disk";
            this.ComponentIcon = Resources.RawSource_201;
            this.ComponentImage = Resources.backup;
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.ETL.ToString();

            this._Instruction =
                new BFileDownloadInstruction(this.ComponentID, Guid.Empty, 
                    DataProviderType.Oracle, string.Empty, string.Empty,
                                             string.Empty, string.Empty,
                                             string.Empty, "{0}", string.Empty, string.Empty);
            this._Output = new BFileDownloadOutput(this.ComponentID, Guid.Empty, 0, string.Empty, string.Empty);

        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DbProvider", ((int) this.DbProvider).ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnStr", this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SelectStatement", this.SelectStatement);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "BFileField", this.BFileField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileNameField", this.FileNameField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "DownloadTo", this.DownloadToFolder);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileNameFormat", this.FileNameFormat);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LogFilePath", this.LogFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ErrorFilePath", this.ErrorFilePath);
            
            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                BFileDownloadComponent wfComponent = base.Instantiate(xNode) as BFileDownloadComponent;

                wfComponent.DbProvider = (DataProviderType) int.Parse(xNode.Attributes.GetNamedItem("DbProvider").Value);
                wfComponent.ConnStr = xNode.Attributes.GetNamedItem("ConnStr").Value;
                wfComponent.SelectStatement = xNode.Attributes.GetNamedItem("SelectStatement").Value;
                wfComponent.BFileField = xNode.Attributes.GetNamedItem("BFileField").Value;
                wfComponent.FileNameField = xNode.Attributes.GetNamedItem("FileNameField").Value;
                wfComponent.DownloadToFolder = xNode.Attributes.GetNamedItem("DownloadTo").Value;
                wfComponent.FileNameFormat = xNode.Attributes.GetNamedItem("FileNameFormat").Value;
                wfComponent.LogFilePath = xNode.Attributes.GetNamedItem("LogFilePath").Value;
                wfComponent.ErrorFilePath = xNode.Attributes.GetNamedItem("ErrorFilePath").Value;

                return wfComponent;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }
    }
}
